package crawler.server;

import crawler.admin.SupportedSiteConst;
import crawler.common.SiteData;
import crawler.common.SourceData;
import crawler.handler.CrawlHandler2;
import crawler.impl.IOneParser;
import crawler.impl.Kenh14Parser;
import crawler.impl.NgoisaoParser;
import crawler.itf.PostImpl;
import crawler.model.SourceModel;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.log4j.Logger;


public class CrawlServer2 implements Runnable {
    private static final Logger logger = Logger.getLogger(CrawlServer2.class);
    private static CrawlServer2 _instance = null;
    private static final Lock createLock_ = new ReentrantLock();
    
    public static CrawlServer2 getInstance() {
        if (_instance == null) {
            createLock_.lock();
            try {
                if (_instance == null) {
                    _instance = new CrawlServer2();
                }
            } finally {
                createLock_.unlock();
            }
        }
        return _instance;
    }
    
    @Override
    public void run() {
        try {
            List<SiteData> lstSao = new ArrayList<>();
            //scan over source urls
            SourceModel sourceModel = SourceModel.getInstance();
            List<SourceData> listSource = sourceModel.getList(0, -1);
            for (SourceData source : listSource) {
                PostImpl parser = null;
                if(source.getDomain().equals(SupportedSiteConst.KENH14)){
                    parser = new Kenh14Parser();
                }else if(source.getDomain().equals(SupportedSiteConst.IONE)){
                    parser = new IOneParser();
                }else if(source.getDomain().equals(SupportedSiteConst.NGOISAONET)){
                    parser = new NgoisaoParser();
                }
                if(parser != null){
                    SiteData siteData = new SiteData(source.getCategoryCode(), source.getUrl(), parser);
                    lstSao.add(siteData);
                }
            }
//            Kenh14Parser kenh14 = new Kenh14Parser();
//            SiteData siteData1 = new SiteData("http://kenh14.vn/star.chn",kenh14);
//            
//            IOneParser ione = new IOneParser();
//            SiteData siteData2 = new SiteData("http://ione.vnexpress.net/tin-tuc/showbiz",ione);
//
//            NgoisaoParser ngoisao = new NgoisaoParser();
//            SiteData siteData3 = new SiteData("http://ngoisao.net/tin-tuc/hau-truong/showbiz-viet",ngoisao);
//
//            lstSao.add(siteData1);
//            lstSao.add(siteData2);
//            lstSao.add(siteData3);
            CrawlHandler2.init(lstSao);
            
        } catch (Exception e) {
            logger.error("Cannot start web server: " + e.getMessage(), e);
            System.exit(1);
        }
    }
    public void stop() throws Exception {
        CrawlHandler2.stopAll();
    }

}